<template>
	<view class="userPage">
		<view class="" v-if="inviteshow">
			<tki-qrcode ref="qrcode" unit='px' background='#fff' foreground='#000' :val="valcode" :loadMake='loadMake' :size="size" />
		</view>
		<text style="font-size: 14px;">{{valcode}}</text>
		<button @click="downloadImage">保存二维码</button>
		<button @click="setClipboard">复制地址</button>

	</view>

</template>

<script>
	import tkiQrcode from "@/components/tki-qrcode/tki-qrcode.vue"
	export default {

		data() {
			return {
				valcode: 'GFD65VC456ER89G74C89V4CX568F9D4BD',
				size: 220,
				loadMake: true,
				inviteshow: false,
				invitecode: '', //邀请码 
			}
		},
		components: {
			tkiQrcode
		},
		onLoad() {
			this.getData();
		},
		onShow() {

		},
		methods: {
			getData: function() {
				const _this = this;
				//获取用户充值地址
				const data = {
					cid: '6'
				}
				this.$request.post("/api/wallet/getAddress", {
						data: data
					})
					.then(res => {
						if (res.data.code == 1) {
							_this.valcode = res.data.data;
							// _this.address = res.data.data;
							_this.inviteshow = true;
						}
					})
					.catch(error => {
						console.error('error:', error);
					});
			},
			downloadImage: function() {
				//this.$refs.qrcode.result;获取二维码图片base64位地址,不能保存在变量中.
				var self = this
				let imgurl = self.$refs.qrcode.result;
				uni.showLoading({
					title: '下载中'
				})

				uni.downloadFile({
					url: imgurl,
					success: (res) => {
						uni.hideLoading();
						uni.saveImageToPhotosAlbum({
							filePath: imgurl, //图片路径
							success: function() {
								// alert('save success');
								uni.showToast({
									title: "保存成功",
									icon: "none",
									duration: 2000
								});
							}
						});
					},
					fail: (err) => {
						alert('downloadFile fail, err is:', err)
					}
				})
			},
			setClipboard: function() {
				var data = this.valcode;
				if (data.length === 0) {
					uni.showModal({
						title: '设置剪贴板失败',
						content: '内容不能为空',
						showCancel: false
					})
				} else {
					uni.setClipboardData({
						data: data,
						success: () => {
							uni.showToast({
								title: "复制成功",
								icon: "none",
								duration: 2000
							});
						},
						fail: () => {
							uni.showModal({
								content: '储存数据失败!',
								showCancel: false
							})
						}
					});
				}
			},
			golink(link) {
				// 跳转到其他页面
				uni.navigateTo({
					url: link
				})
			},
			closeRightDrawer() { //关闭抽屉式导航
				this.rightDrawerVisible = false;
			},
			showRightDrawer() { //显示抽屉式导航
				this.rightDrawerVisible = true;
			},
		},
		onBackPress() {
			// 返回按钮监听
			if (this.rightDrawerVisible) {
				this.rightDrawerVisible = false;
				return true;
			}
		},

	}
</script>

<style lang="less">
	.userPage {
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		border-top: 5px solid #F1F1F1;

		>text {
			text-align: center;
			margin: 30px auto;
		}

		>button {
			background: #03AD8F;
			color: #fff;
			font-size: 32upx;
			height: 92upx;
			width: 70%;
			text-align: center;
			box-shadow: 0px 3px 15px rgba(46, 255, 255, 0.74);
			line-height: 92upx;
			border-radius: 16upx;
			margin-bottom: 30px;
		}
	}

	.uni-list-cell-navigate {
		text-align: center;
		font-size: 14px;
		line-height: 80upx;
		border-bottom: 1px solid #F1F1F1;
	}
</style>
